package com.ktjy.controller;

import com.ktjy.service.IUploadService;
import com.ktjy.service.impl.MinioUploadServiceImpl;
import com.ktjy.utils.AjaxResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class UploadController {

//    private final MinioUploadServiceImpl minioUploadServiceImpl;
//
//    public UploadController(MinioUploadServiceImpl minioUploadServiceImpl) {
//        this.minioUploadServiceImpl = minioUploadServiceImpl;
//    }

//    @Autowired
//    private MinioUploadServiceImpl minioUploadServiceImpl;

    // 使用lombok构造函数注入，避免使用@Autowired，避免循环依赖
    private final IUploadService uploadService; // 使用接口而非具体实现

    @PostMapping("/upload")
    public AjaxResult uploadFile(@RequestParam("file")MultipartFile file) {
        // 判断文件是否为空
        if (file == null || file.isEmpty()) {
            return AjaxResult.error("请选择要上传的文件");
        }

        try {
            String fileUrl = uploadService.uploadFile(file);
            return AjaxResult.success("上传成功",fileUrl);
        } catch (Exception e) {
            return AjaxResult.error("上传失败");
        }
    }
}
